home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
353_01
/
chap12.txt
< prev
next >
Wrap
Text File
|
1992-01-18
|
26KB
|
589 lines
Chapter 12
FLYAWAY ADVENTURE GAME
Now that you have learned lots of things about C++, and know how
to write and use a single isolated class, you have the problem of
how to build a program with several classes that work together to
accomplish some task. After some amount of thought, it seems that
an adventure game would be a good candidate for a relatively large
example program. It has lots of input and output and requires a
good deal of flexibility while running since there are so many
things that can be included in the game as obstacles, mazes, items
to find, and puzzles to solve.
The adventure game presented in this chapter is unique as far as
I know, since I have never heard of another adventure game
featuring an airport. The location is not nearly as important as
the code used to get through the airport. You are advised to play
the game to get familiar with what the code does, then study the
code to see how it works. Finally, you are given an assignment to
extend the code which will be the real test of whether you
understand its operation.
PLAYING THE GAME
_________________________________________________________________
Prior to studying the source code for this game, ===============
it would be to your advantage to spend some time FLYAWAY.EXE
playing the game to get familiar with what the ===============
game does. Load the file FLYAWAY.EXE and begin
the adventure through the airport. The
executable file is precompiled for you so you can begin executing
the program before you have to compile and link the whole thing.
The entire program is composed of 15 files and will take a little
effort on your part to properly compile and link it, but that will
come later.
If you have played adventure games before, sometimes called
interactive fiction, you should begin trying various commands to
find your way through the airport to your proper plane. If you
have not played before, a few hints are in order concerning how to
play the game.
The object of the game is to get to your proper plane on time so
you can fly away to your vacation. Of course there a few obstacles
and problems along the way and they will be brought up at the
appropriate time. It will be up to you to solve the puzzles
associated with each problem. To add a little excitement, you only
have about twenty-five minutes to get to your plane, with each move
Page 12-1
Chapter 12 - Flyaway Adventure Game
taking a minute, so you must hurry. Of course, just getting to the
plane on time is not enough, there are a few additional
requirements. You will find what they are as you progress through
the game. You will probably find it necessary to restart the game
many times before you arrive at your destination unscathed and on
time.
THE METHOD OF PLAY
_________________________________________________________________
The method of play is extremely simple. You simply wander around
the airport looking for things to do and places to go. You move
around the airport by giving the system commands to go in a
direction with four choices available, north, south, east, or west.
You can abbreviate any of these four direction commands to the
first letter only, and you can use either upper or lower case. The
system may move you to another area of the airport, or it may tell
you that you can't go that way. Try loading the game now and
typing the four directions once each to see what happens. If this
is not clear, enter the word help to get you started.
In addition to moving around, you can pick up items or ask for more
information in any of the rooms. Try telling the system to look
around the room and see what additional information it gives you
for each room, some of the clues for solving the puzzle are given
in the clues issued in response to a look command. Another
important command is inventory which will give you a list of the
items you possess at any given point in time. Type the word
inventory at this time to see what items you possess.
The remainder of the commands consist of two words, a verb and a
noun. These can be given in either order, since the system is
smart enough to know the difference, and additional words may be
given following the legal words. If you give the system a command
that is not in its limited vocabulary, it will tell you it doesn't
understand that word. Try telling the system to drop an item you
possess, or get an item that is located in the room you are
currently in.
Several friends have played this game with no more knowledge than
you have been given. One solved it in 40 minutes, but most took
about an hour to complete the game. After you play the game for
awhile, return to the text and we will study the source code for
the game. The entire source code for the game is on your
distribution disk. The game was purposely kept small so the code
could be easily grasped by a programming student. There is no
reason the game could not have been made much larger by the
addition of more rooms, items, and traps. You may choose to do
just that to gain experience in working with C++.
Page 12-2
Chapter 12 - Flyaway Adventure Game
A FEW SPECIAL CONSTANTS
_________________________________________________________________
The file named FLYAWAY.H contains the ===============
definitions for TRUE and FALSE as well as the FLYAWAY.H
enumerated type defining the legal dictionary of ===============
words for use in playing the game. The list was
started at a value of 1 so the value of zero can
be used to indicate that the word in question was not in the
library and hence not a legal word for use with the game.
The #ifndef in line 5 is required because this header file is
included in many of the other files and if it is included more than
once, there will be a multiple definition, and hence an error. A
class only needs to be defined once, so after it is defined by one
of the includes, the name ITEMS_H will be defined and any other
defines will be ignored. This is necessary because of the separate
compilation capability of C++. This was described in more detail
near the end of chapter 7.
THE FIRST CLASS - clock
_________________________________________________________________
Examine the file named CLOCK.H for the ===============
definition of the clock class. This is the CLOCK.H
class for the game clock, and only one instance ===============
of this class will be used. It will be used for
the object time_of_day defined in line 23 of
FLYAWAY.CPP.
The class is very simple, consisting of only two variables, the
hour and the minute, and four methods. The first method is the
constructor used to initialize the clock to 8:51 as you can see if
you refer to the implementation of this class in the file named
CLOCK.CPP. The next two methods are used to get the current values
of the two variables. The final method is much more interesting
since it does more. It updates the time of day clock and outputs
the user prompt to ask for the next command. This may not be the
best place to output the user prompt since this class is devoted
to the time of day and associated operations, but this was chosen
as the place to do it since the time of day is part of the user
prompt. You will notice that the clock was initialized to 8:51,
but the first time output was 8:52 when you played the game. In
order to simplify the coding later, when we need to decide if we
made it to the plane on time, the time was incremented at the
beginning of each game move. The time is therefore the same when
the command is entered and when it is executed, hence the time is
incremented prior to even the first output.
The clock class is by far the simplest class in the adventure game